<template>
  <view class="ts-column">
    <view class="top"></view>
    <view class="ts-column ts-center" style="margin-top: -150upx;">
      <view class="ts-row">
        <image style="width: 200upx; height: 200upx;" src="../../../../static/images/logo.png" mode=""></image>
      </view>
      <view class="ts-row">
        产教融合
      </view>
    </view>
    <qrcode :val="qrcodeLink" :size="qrsize" ref="qrcode"></qrcode>

    <!-- #ifdef APP-PLUS -->
    <view class="ts-row ts-padding ts-center" v-if="providerList.length > 0">
      <block v-for="(value,index) in providerList" :key="index">
        <view class="ts-row">
          <view class="ts-padding">
            <image v-if="value" class="icon" :src="'../../../../static/app-plus/providers/'+value.icon+'.png'" @tap="share(value)" />
            </image>
          </view>
        </view>
      </block>
    </view>
    <!-- #endif -->

  </view>
</template>

<script>
  import Qrcode from '@/components/qrcode/qrcode.vue'
  export default {
    components: {
      Qrcode,
    },
    data() {
      return {
        qrcodeLink: 'http://www.zengqs.com/share?id=3228969',
        qrsize: uni.upx2px(300),


        providerList :[],
        shareType: 0,
        shareImage: 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/app/share-logo@3.png',
        shareTitle: '欢迎体验优职道',
        shareText: '专注在校学生的实习、就业，破解企业招工难题，提供精准就业、精准招聘服务！',
        shareLink: 'http://www.zengqs.com', //生成的二维码的链接，点解分享链接到改页面
        
      };
    },
    methods: {
     // #ifdef APP-PLUS
      async share(e) {
        console.log('分享通道:' + e.id + '； 分享类型:' + this.shareType);
        if (!this.shareText && (this.shareType === 1 || this.shareType === 0)) {
          uni.showModal({
            content: '分享内容不能为空',
            showCancel: false
          })
          return;
        }

        if (!this.shareImage && (this.shareType === 2 || this.shareType === 0)) {
          uni.showModal({
            content: '分享图片不能为空',
            showCancel: false
          })
          return;
        }

        if (e.id === 'qq') {
          this.shareType = 1; //QQ不支持图文分享
        }

        let shareOPtions = {
          provider: e.id,
          scene: e.type && e.type === 'WXSenceTimeline' ? 'WXSenceTimeline' : 'WXSceneSession', 
          //WXSceneSession”分享到聊天界面，“WXSenceTimeline”分享到朋友圈，“WXSceneFavorite”分享到微信收藏     
          type: this.shareType,
          success: (e) => {
            console.log('success', e);
            uni.showModal({
              content: '分享成功',
              showCancel: false
            })
          },
          fail: (e) => {
            console.log('fail', e)
            uni.showModal({
              content: e.errMsg,
              showCancel: false
            })
          },
          complete: function() {
            console.log('分享操作结束!')
          }
        }

        switch (this.shareType) {
          case 0: //图文
            shareOPtions.summary = this.shareText;
            shareOPtions.imageUrl = this.shareImage;
            shareOPtions.title = this.shareTitle;
            shareOPtions.href = this.shareLink;
            break;
          case 1: //纯文字
            shareOPtions.summary = this.shareText;
            break;
          case 2: //纯图片
            shareOPtions.imageUrl = this.shareImage;
            break;
          case 5: //小程序
            shareOPtions.imageUrl = this.shareImage;
            shareOPtions.title = this.shareTitle;
            shareOPtions.miniProgram = {
              id: 'wx86ab541f20e43dc4',
              path: '/pages/tabbar/home/index',
              webUrl: this.shareLink,
              type: 0
            };
            break;
          default:
            break;
        }

        if (shareOPtions.type === 0 && plus.os.name === 'iOS') { //如果是图文分享，且是ios平台，则压缩图片 
          shareOPtions.imageUrl = await this.compress();
        }
        if (shareOPtions.type === 1 && shareOPtions.provider === 'qq') { //如果是分享文字到qq，则必须加上href和title
          shareOPtions.href = this.shareLink;
          shareOPtions.title = this.shareTitle;
        }


        uni.share(shareOPtions);
      },
     // #endif
    },
    onLoad() {
      //必须延时，否则报错
      setTimeout(() => {
        this.$refs.qrcode.creatQrcode();
        //console.log(this.$refs.qrcode.img);
      }, 100)

      //#ifdef APP-PLUS
      uni.getProvider({
        service: 'share',
        success: (e) => {
          console.log('success', JSON.stringify(e.provider));
          let data = []
          for (let i = 0; i < e.provider.length; i++) {
            switch (e.provider[i]) {
              case 'weixin':
                data.push({
                  name: '分享到微信好友',
                  id: 'weixin',
                  icon: 'weixin',
                  type: 'WXSceneSession', //default value
                  sort: 0
                })
                data.push({
                  name: '分享到微信朋友圈',
                  id: 'weixin',
                  icon: 'wechat',
                  type: 'WXSenceTimeline',
                  sort: 1
                })
                break;

              case 'qq':
                data.push({
                  name: '分享到QQ',
                  id: 'qq',
                  icon: 'qq',
                  sort: 2
                })
                break;
              case 'sinaweibo':
                data.push({
                  name: '分享到新浪微博',
                  id: 'sinaweibo',
                  icon: 'sinaweibo',
                  sort: 3
                })
                break;
              default:
                break;
            }
          }
          this.providerList = data.sort((x, y) => {
            return x.sort - y.sort
          });
        },
        fail: (e) => {
          console.log('获取登录通道失败', e);
          uni.showModal({
            content: '获取登录通道失败',
            showCancel: false
          })
        }
      });
      //#endif
    }
  }
</script>

<style lang="scss">
  .top {
    width: 100%;
    height: 300upx;
    /* #ifdef  APP-PLUS  */
    // background: url('../../../../static/app-plus/banner.png') no-repeat;
    background: url('http://pds.jyt123.com/wxtest/banner.png') no-repeat;
    /* #endif */
    /* #ifndef APP-PLUS */
    background: url('http://pds.jyt123.com/wxtest/banner.png') no-repeat;
    /* #endif */
    background-size: 100%;
    background-position: center center;
  }
  
  .icon{
    width: 100upx;
    height: 100upx;
  }
</style>
